var $ = require("helper.js")
function change(goods, buynumber) {
  buynumber = parseInt(buynumber)
  var id=goods.id
  if (id == null) {
    return;
  }
 
  var shopingcar = $.cache("shopingcar")
  if (shopingcar == null) {
    shopingcar = []
  }
  var _index = -1
  for (var i in shopingcar) {
    if (id == shopingcar[i].id) {
      _index = i
    }

  }
  var newnumber=0;
  if (_index != -1) {
    newnumber = shopingcar[_index]['buynumber'] + buynumber
    shopingcar[_index]['buynumber'] = newnumber
  } else {
    newnumber = buynumber
    shopingcar.push({ id: id, buynumber: newnumber })
  }
  if (newnumber > goods['number']) {
    getApp().toast(goods.name + "库存不足")
    return;
  }
  if (newnumber <1) {
    del(goods.id)
    return;
  }
  $.cache("shopingcar", shopingcar)
}

function get() {
  return $.cache("shopingcar");
}
function clear() {
  $.cache("shopingcar", "del");
}


function del(id,cb) {
 


  var shopingcar = $.cache("shopingcar")
  if (shopingcar == null) {
    shopingcar = []
  }
  var newlist = []
  for (var i in shopingcar) {
    if (id != shopingcar[i].id) {
      newlist.push(shopingcar[i])
    }
  }
  $.cache("shopingcar", newlist);
 
}


function pay(orderno,cb){
  
}

module.exports.change = change
module.exports.pay = pay

module.exports.del = del
module.exports.get = get
module.exports.clear = clear
